package com.lmx.cloud;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.charset.StandardCharsets;

public class test {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9998);

        while (true){
            //准备接收
            byte[] container = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(container,0,container.length);
            datagramSocket.receive(datagramPacket);//阻塞式接收

            //断开连接
            byte[] datas = datagramPacket.getData();
            String data = new String(datas, 0, datagramPacket.getLength(), StandardCharsets.UTF_8);
            if (data.trim().equals("bye")){
                break;
            }else{
                System.out.println(data);
            }
        }

        datagramSocket.close();
    }
}